package stage1.module3.homework4;

import java.awt.desktop.SystemEventListener;
import java.util.ArrayList;
import java.util.Scanner;

//修改学生姓名和年龄，不可更改学号
public class Modify {
    ArrayList<Student> students;
    public Modify(ArrayList<Student> students){
        this.students = students;
        Scanner sc = new Scanner(System.in);
        int id;
        //判断是否有该学号
        A:while (true){
            System.out.println("请输入想要修改的学生学号");
            id = sc.nextInt();
            for(Student s : students){
                if(s.getId() == id){
                    break A;
                }
            }
            System.out.println("没有该学号");
        }
        //存在该学生则继续修改信息
        System.out.println("请输入想要修改的信息");
        System.out.println("1姓名");
        System.out.println("2年龄");
        switch (sc.nextInt()){
            case 1:
                System.out.println("请输入新的姓名");
                String newName = sc.next();
                modifyName(id,newName);
                break;
            case 2:
                System.out.println("请输入新的年龄");
                int newAge = sc.nextInt();
                modifyAge(id,newAge);
                break;
        }
    }

    public void modifyName(int id, String name){
        for(Student s : students){
            if(s.getId() == id){
                s.setName(name);
            }
        }
    }

    public void modifyAge(int id, int age){
        for(Student s : students){
            if(s.getId() == id){
                s.setAge(age);
            }
        }
    }
}
